<?php
abstract class BaseModel extends Model{

    protected function table(){
        return "";
    }

    protected function primary_key_name(){
        return "";
    }

    public function delete($id) {
        $this->db->query("DELETE FROM " . DB_PREFIX .$this->table(). " WHERE " . $this->primary_key_name() . " = '" . (int)$id . "'");
    }

    public function getItem($id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX .$this->table(). "  WHERE " .  $this->primary_key_name() ." = '" . (int)$id . "'");

        return $query->row;
    }


    public function getList($data = array()) {
        $sql = "SELECT *  FROM " . DB_PREFIX . $this->table() . "  WHERE 1=1 ";

        $sort_data = array(
            $this->primary_key_name()
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY " . $sort_data[0];
        }

        if (isset($data['order']) && ($data['order'] == 'ASC')) {
            $sql .= " ASC";
        } else {
            $sql .= " DESC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }
    public function getCount() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " .$this->table() );

        return $query->row['total'];
    }


}